package app.cn.com.school.activity

import android.os.Bundle
import android.support.v4.app.Fragment
import android.view.Gravity
import android.view.View
import android.widget.TextView
import android.widget.Toast
import app.cn.com.school.R
import app.cn.com.school.databinding.ActivityHomeBinding
import app.cn.com.school.fragment.*
import app.cn.com.school.tools.AppManager
import app.cn.com.school.tools.XPermissionUtils
import kotlin.concurrent.thread


class HomeActivity : BaseActivity<ActivityHomeBinding>(), View.OnClickListener {
    override fun onClick(v: View?) {
        when (v!!.id) {
            R.id.movie -> currentIndex = 0
            R.id.tv -> currentIndex = 1
            R.id.menu_iv -> currentIndex = 2
            R.id.variety -> currentIndex = 3
            R.id.cartoon -> currentIndex = 4
        }
        isMenuSelect = false
        showCurrentFragment(currentIndex)
    }

    override fun initBindingVar() {
    }

    override fun initTitle() {
    }

    private val fragments = ArrayList<Fragment>()
    var homeFg: HomeFragment? = null
    var mineFg: MineFragment? = null
    var usedFg: UsedFragment? = null
    var welfareFg: WelfareFragment? = null
    var praiseFg: PraiseFragment? = null
    private var views = ArrayList<TextView>()
    //    当前选中的views的下标
    private var currentIndex = 0
    //    旧的views下标
    private var oldIndex = 0
    private var isMenuSelect = false

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        initBinding(R.layout.activity_home)
//        LocationTools().set(this@MainActivity) //定位
        if (savedInstanceState == null) {
            initFragments()
            initViews()
        }
    }

    private fun initViews() {
        binding.movie.setOnClickListener(this)
        binding.tv.setOnClickListener(this)
        binding.variety.setOnClickListener(this)
        binding.cartoon.setOnClickListener(this)
        binding.menuIv.setOnClickListener(this)
        binding.movie.isSelected = true
        views.add(binding.movie)
        views.add(binding.tv)
        views.add(binding.menuIv)
        views.add(binding.variety)
        views.add(binding.cartoon)
    }

    private fun initFragments() {
        homeFg = HomeFragment.newInstance("", "")
        welfareFg = WelfareFragment.newInstance("", "")
        usedFg = UsedFragment.newInstance("", "")
        praiseFg = PraiseFragment.newInstance("", "")
        mineFg = MineFragment.newInstance("", "")
        fragments.add(homeFg as Fragment)
        fragments.add(usedFg as Fragment)
        fragments.add(welfareFg as Fragment)
        fragments.add(praiseFg as Fragment)
        fragments.add(mineFg as Fragment)
        supportFragmentManager.beginTransaction().add(R.id.content, homeFg).add(R.id.content, welfareFg)
                .hide(welfareFg).show(homeFg).commit()
    }


    private fun showCurrentFragment(currentIndex: Int) {
        if (currentIndex != oldIndex) {
            views[oldIndex].isSelected = false
            views[currentIndex].isSelected = true
            var ft = supportFragmentManager.beginTransaction()
            ft.hide(fragments[oldIndex])
            if (!fragments[currentIndex].isAdded) {
                ft.add(R.id.content, fragments[currentIndex])
            }
            ft.show(fragments[currentIndex]).commit()
            oldIndex = currentIndex
        }
    }

    override fun onDestroy() {
        super.onDestroy()
        AppManager.getAppManager().finishActivity(this)
    }

    @Volatile
    var backTime = 0

    override fun onBackPressed() {
        if (backTime == 0) {
            val t = Toast.makeText(this, "再按一次退出App", Toast.LENGTH_SHORT)
            t.setGravity(Gravity.CENTER, 0, 0)
            t.show()
            backTime = 1
            thread {
                Thread.sleep(1000)
                backTime = 0
            }
        } else {
            super.onBackPressed()
        }
    }

    override fun onResume() {
        super.onResume()
        AppManager.getAppManager().addActivity(this)
    }

    override fun onRequestPermissionsResult(requestCode: Int, permissions: Array<out String>, grantResults: IntArray) {
        XPermissionUtils.onRequestPermissionsResult(requestCode, permissions, grantResults)
    }


}


